package com.benimetum.beans;

import com.benimetum.managers.LoginManager;
import com.benimetum.managers.StudentManager;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import org.primefaces.model.UploadedFile;



/**
 *
 * @author Ethem Cem Ozkan
 * @email ethemcem.ozkan@gmail.com
 * No 07110604
 */
@ManagedBean(name="ControlPanelBean")
@SessionScoped
public class ControlPanelBean implements Serializable{
    private String ad;
    private String soyad;
    private String email;
    private String sifreEski;
    private String sifre;
    private String sifre2;
    private boolean error;
    private boolean resim;
    private String errorMessage;
    private UploadedFile file;

    public ControlPanelBean() {
	  errorMessage=null;
          email="";
          sifre="";
          sifreEski="";
    }
    
    public String getErrorMessage() {
	  return errorMessage;
    }

    public UploadedFile getFile() {
	  return file;
    }

    public boolean getResim() {
	  return resim;
    }
    
    public String getAd() {
	  return ad;
    }

    public String getEmail() {
	  return email;
    }

    public String getSifre() {
	  return sifre;
    }

    public String getSifre2() {
	  return sifre2;
    }

    public String getSifreEski() {
	  return sifreEski;
    }

    public String getSoyad() {
	  return soyad;
    }

    public void setAd(String ad) {
	  this.ad = ad;
    }

    public void setEmail(String email) {
	  this.email = email;
    }

    public void setSifre(String sifre) {
	  this.sifre = sifre;
    }

    public void setFile(UploadedFile file) {
	  this.file = file;
    }

    public void setSifre2(String sifre2) {
	  this.sifre2 = sifre2;
    }

    public void setErrorMessage(String errorMessage) {
	  this.errorMessage = errorMessage;
    }

    public void setError(boolean error) {
	  this.error = error;
    }

    public void setSifreEski(String sifreEski) {
	  this.sifreEski = sifreEski;
    }

    public void setResim(boolean resim) {
	  this.resim = resim;
    }

    public void setSoyad(String soyad) {
	  this.soyad = soyad;
    }
    
    public void clickResim(){
	  if(resim==false)
		resim=true;
	  else
		resim=false;
    }
    
    public String createEmailLabel(){
	  return ": "+LoginManager.getUser().getEmail();
    }
    
    public String createSifreLabel(){
	  return ": ********";
    }
    
    public String createAdLabel(){
	  return ": "+LoginManager.getUser().getFirstName();
    }
    
    public String createSoyadLabel(){
	  return ": "+LoginManager.getUser().getLastName();
    }
    
    public void changeEmail(){
	  error=StudentManager.updateEmail(LoginManager.getUser().getId(), email);
	  if(error=true){
		errorMessage="Emailiniz "+email+" olarak değiştirildi.";
		LoginManager.getUser().setEmail(email);
	  }
	  else
		errorMessage="Hata oluştu.";
    }
    
    public void changeSifre(){
	  if(sifre.equals(sifre2)&&LoginManager.getPassword(LoginManager.getUser().getId()).equals(sifreEski)){
		error=StudentManager.updatePassword(LoginManager.getUser().getId(), sifre);
		if(error=true)
		    errorMessage="Şifreniz başarıyla değiştirildi.";
		else
		    errorMessage="Hata oluştu.";
	  }		
	  else{
		error=false;
		errorMessage="Girdiğiniz şifre yanlış.";
	  }
		
    }
    
    public void changeAd(){
	  error=StudentManager.updateFirstName(LoginManager.getUser().getId(), ad);
	  if(error=true){
		errorMessage="Adınız "+ad+" olarak değiştirildi.";
		LoginManager.getUser().setFirstName(ad);
	  }
	  else
		errorMessage="Hata oluştu.";
    }
    
    public void changeSoyad(){
	  error=StudentManager.updateLastName(LoginManager.getUser().getId(), soyad);
	  if(error=true){
		errorMessage="Soyadınız "+soyad+" olarak değiştirildi.";
		LoginManager.getUser().setLastName(soyad);
	  }
	  else
		errorMessage="Hata oluştu";
    }
    
    public void changePicture(){
	  FacesMessage msg = new FacesMessage("Succesful", file.getFileName() + " is uploaded.");  
        FacesContext.getCurrentInstance().addMessage(null, msg);  
	  File f=new File("tmp.jpg");
	  try {
		InputStream inputStream=file.getInputstream();
		OutputStream out=new FileOutputStream(f);
		byte buf[]=new byte[1024];
		int len;
		while((len=inputStream.read(buf))>0)
		out.write(buf,0,len);
		out.close();
		inputStream.close();
		StudentManager.changeProfilePicture(f, LoginManager.getUser().getId());
	  } catch (IOException ex) {
		Logger.getLogger(ControlPanelBean.class.getName()).log(Level.SEVERE, null, ex);
	  }
    }
}
